package com.msmall.community.bbs.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.community.bbs.dao.FavoriteDao;
import com.msmall.community.bbs.model.Favorite;
import com.msmall.community.bbs.service.FavoriteManager;


public class FavoriteManagerImpl extends BaseManagerImpl<Favorite, Long> implements FavoriteManager{

	private static final Log log = LogFactory.getLog(FavoriteManagerImpl.class);
	
	private FavoriteDao favoriteDao;
	
	public void setFavoriteDao(FavoriteDao favoriteDao) {
		this.favoriteDao = favoriteDao;
	}
	public void save(Long userid, Long topicId) throws Exception{
		try{
			Favorite old = getFavorite(userid, topicId);
			if(old != null)
				return;
			Favorite favorite = new Favorite();
			favorite.setUserid(userid);
			favorite.setTopicid(topicId);
			favorite.setCreateTime(new Date());
			
			favoriteDao.save(favorite);
		}catch(Exception e){
			throw e;
		}
	}
	public void getPagination(PaginationSupport page, Long userid) {
		page.setClazz(Favorite.class);
		String queryString = " from Favorite f where f.userid='" + userid + "' order by f.createTime desc";
		page.setQueryString(queryString);
		favoriteDao.getPagination(page);
	}
	public Favorite getFavorite(Long userid, Long topicId) throws Exception{
		return favoriteDao.getFavorite(userid, topicId);
	}
	/**
	 * 获得用户所有收藏的帖子
	 */
	public List<Favorite> getAllByUserid(Long userid){
		try {
			return favoriteDao.getAllByUserid(userid);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
}
